//
//  BodyCheckCheckResultDetailViewController.m
//  TryShopping
//
//  Created by panquanReabam.com on 16/9/8.
//  Copyright © 2016年 RuiBen. All rights reserved.
//

#import "BodyCheckCheckResultDetailViewController.h"
#import "MJRefresh.h"
#import "OrderDetailView.h"
#import "BodyCheckCheckResultDetailModel.h"
#import "BodyCheckCheckResultDetailView.h"
#import "PrinterListViewController.h"
#import "SVProgressHUD.h"
@interface BodyCheckCheckResultDetailViewController ()<MBProgressHUDDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate,UITextFieldDelegate,UITextViewDelegate,UISearchBarDelegate,UIScrollViewDelegate,UIWebViewDelegate>
@end
@implementation BodyCheckCheckResultDetailViewController
{

    BodyCheckCheckResultDetailView*DetailView;
    UIWebView*webview;
    UIScrollView*mainScroll;
    NSDictionary*dataDict;
}
@synthesize alt = alt;

-(void)BackAction:(UIButton *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    self.navigationController.delegate = nil;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self update:@""];
}

- (void)update:(NSString *)str{
    
    [self setNavi];
    [mainScroll removeFromSuperview];
    mainScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0, ScreenWidth, ScreenHeight)];
    [mainScroll setBackgroundColor: [UIColor whiteColor]];
    mainScroll.delegate = self;
    mainScroll.userInteractionEnabled = YES;
    mainScroll.scrollEnabled = YES;
    mainScroll.showsVerticalScrollIndicator = FALSE;
    mainScroll.showsHorizontalScrollIndicator = FALSE;
    [self.view addSubview:mainScroll];
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:Token forKey:@"tokenId"];
    [param setValue:self.orderId forKey:@"reportId"];
    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
    [HttpRequest requestData:@"BodyCheck/CheckResult/Detail" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            NSLog(@"-----------%@", severDataDict.dataDict);
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                dataDict=severDataDict.dataDict;
                
                DetailView = [[BodyCheckCheckResultDetailView alloc]init];
//                DetailView.mDelegate=self;
                BodyCheckCheckResultDetailModel*dataModel=[BodyCheckCheckResultDetailModel dataModelWithBodyCheckCheckResultDetailDict:severDataDict.dataDict];
                DetailView.model=dataModel;
                DetailView.frame=CGRectMake(0, 0, ScreenWidth, DetailView.heightView.bottom);
                [mainScroll addSubview:DetailView];
 
                
                
                webview  = [[UIWebView   alloc]  initWithFrame:CGRectMake( 0,  460,  ScreenWidth ,  ScreenHeight-ScreenHeight/2)];
//               webview.scalesPageToFit = TRUE;
                [webview   setUserInteractionEnabled: NO ];	 //是否支持交互
                [webview   setDelegate: self ];				 //委托
                [webview   setOpaque: NO ];
                [webview setNeedsLayout];
                [webview layoutIfNeeded];//透明
                webview.backgroundColor=[UIColor whiteColor];
                [mainScroll  addSubview : webview];
             
               [webview loadHTMLString:[severDataDict.dataDict objectForKey:@"projectDetail"] baseURL:[NSURL fileURLWithPath: [[NSBundle mainBundle] bundlePath]]];
            
                
            
            }else{
                [Global hideProgressHUDInternal:self.view];
               
                
            }
        }else
        {
            [Global hideProgressHUDInternal:self.view];
            [Global showTextHudSub:severDataDict.resultStr view:self.view];
        }
    }];
}
- (void)webViewDidFinishLoad:(UIWebView *)wb
{
//    //方法1
//    CGFloat documentWidth = [[wb stringByEvaluatingJavaScriptFromString:@"document.getElementById('content').offsetWidth"] floatValue];
//    CGFloat documentHeight = [[wb stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"content\").offsetHeight;"] floatValue];
//    NSLog(@"documentSize = {%f, %f}", documentWidth, documentHeight);
    
    //方法2
    CGRect frame = wb.frame;
    frame.size.width = ScreenWidth;
    frame.size.height = 1;
    
    //    wb.scrollView.scrollEnabled = NO;
    wb.frame = frame;
    
    frame.size.height = wb.scrollView.contentSize.height;
    
    NSLog(@"frame = %@", [NSValue valueWithCGRect:frame]);
    wb.frame = frame;
    
     [mainScroll setContentSize:CGSizeMake(ScreenWidth, wb.bottom)];
}
- (void)showAlertWait
{
    alt = [[UIAlertView alloc] initWithTitle:@"网络不稳定 ！" message:@"请确认网络" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
    
}

- (void)showAlertMessage:(NSString *)msg
{
    alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
}
- (void)hideAlert
{
    if (alt != nil)
    {
        [alt dismissWithClickedButtonIndex:0 animated:YES];
        alt = nil;
    }
}
-(void)setNavi{
    [[self rdv_tabBarController] setTabBarHidden:YES];
    self.navigationController.navigationBarHidden=NO;
    self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:Color];
    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.textAlignment=NSTextAlignmentLeft;
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    self.navigationController.delegate = self;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage =[[UIImage alloc] init];
    self.view.backgroundColor=[UIColor whiteColor];
    self.title=@"报告详情";
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     [UIColor blackColor],
                                                                     UITextAttributeTextColor,
                                                                     [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1],
                                                                     UITextAttributeTextShadowColor,
                                                                     [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                     UITextAttributeTextShadowOffset,
                                                                     [UIFont fontWithName:@"Helvetica Neue" size:tetlefot],
                                                                     UITextAttributeFont,nil]];
    
}

@end
